package giaodien;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;

import java.awt.Color;

import javax.swing.JButton;

import java.awt.Font;

import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.util.Vector;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;
import javax.swing.JScrollPane;

import controller.NhanVienController;
import controller.UserController;

public class NhanVien extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					NhanVien frame = new NhanVien("");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public NhanVien(final String user) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 666, 374);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Qu\u1EA3n l\u00FD nh\u00E2n vi\u00EAn", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(128, 0, 128)));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Danh s\u00E1ch nh\u00E2n vi\u00EAn", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(128, 0, 128)));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE))
		);
		java.util.List<model.NhanVien> list = NhanVienController
				.getDSNhanVien();
		Vector<Vector> rowData = new Vector<Vector>();
		Vector<String> name = new Vector<String>();
		name.addElement("Mã nhân viên");
		name.addElement("Tên nhân viên");
		name.addElement("Chức vụ");
		name.addElement("Địa chỉ");
		name.addElement("Số điện thoại");
		name.addElement("Lương cơ bản");
		Vector v;
		NumberFormat n = NumberFormat.getInstance();
		for (int i = 0; i < list.size(); i++) {
			v = new Vector();
			v.add(list.get(i).getMaNV());
			v.add(list.get(i).getTenNV());
			v.add(list.get(i).getChucVu());
			v.add(list.get(i).getDiaChi());
			v.add((list.get(i).getSdt()));
			v.add((list.get(i).getLuongCB()));
			rowData.add(v);
		}
		System.out.println(list.size());
		panel_1.setLayout(null);
		table = new JTable(rowData, name);
		JScrollPane pane = new JScrollPane(table);
		pane.setBounds(10, 27, 612, 295);
		panel_1.add(pane);
		
		JButton btnThm = new JButton("Thêm");
		btnThm.setBounds(11, 21, 143, 41);
		btnThm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ThemNhanVien nv = new ThemNhanVien();
				nv.setVisible(true); 
			}
		});
		panel.setLayout(null);
		btnThm.setIcon(new ImageIcon(NhanVien.class.getResource("/icon/add-icon.png")));
		btnThm.setForeground(new Color(139, 0, 0));
		btnThm.setFont(new Font("Tahoma", Font.BOLD, 13));
		panel.add(btnThm);
		
		JButton btnTmSa = new JButton("Tìm - Sửa");
		btnTmSa.setBounds(165, 21, 143, 41);
		btnTmSa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SuaNhanVien s = new SuaNhanVien();
				s.setVisible(true);
			}
		});
		btnTmSa.setIcon(new ImageIcon(NhanVien.class.getResource("/icon/edit-icon.png")));
		btnTmSa.setForeground(new Color(139, 0, 0));
		btnTmSa.setFont(new Font("Tahoma", Font.BOLD, 13));
		panel.add(btnTmSa);
		
		JButton btnXa = new JButton("Xóa");
		btnXa.setBounds(319, 21, 143, 41);
		btnXa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String phanquyen = UserController.getUser(user).getPhanQuyen();
				if(phanquyen.equals("quanly")== true){
					XoaNhanVien x = new XoaNhanVien();
					x.setVisible(true);
				}else{
					KoDuocQLNV k = new KoDuocQLNV();
					k.setVisible(true);
				}
			}
		});
		btnXa.setIcon(new ImageIcon(NhanVien.class.getResource("/icon/close-icon.png")));
		btnXa.setForeground(new Color(139, 0, 0));
		btnXa.setFont(new Font("Tahoma", Font.BOLD, 13));
		panel.add(btnXa);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.setBounds(473, 21, 143, 41);
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				NhanVien s = new NhanVien(user);
				s.setVisible(true);
			}
		});
		btnRefresh.setIcon(new ImageIcon(NhanVien.class.getResource("/icon/Button-Refresh-icon.png")));
		btnRefresh.setForeground(new Color(139, 0, 0));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD, 13));
		panel.add(btnRefresh);
		contentPane.setLayout(gl_contentPane);
	}
}
